package nc.ui.mlr.jgd;

import java.util.ArrayList;

import nc.jdbc.framework.processor.BeanListProcessor;
import nc.ui.mlr.pub.MLRProxy;
import nc.ui.pf.pub.PfUIDataCache;
import nc.vo.mlr.jgd.PortbillHVO;
import nc.vo.pub.SuperVO;
import nc.vo.pub.billtype.BilltypeVO;

public class BusinessDelegator extends nc.ui.trade.bsdelegate.BusinessDelegator {

	@Override
	public SuperVO[] queryHeadAllData(Class headClass, String strBillType,
			String strWhere) throws Exception {
		BilltypeVO billVo = PfUIDataCache.getBillType(strBillType);
		if (billVo.getWherestring() != null
				&& billVo.getWherestring().length() != 0) {
			if (strWhere != null)
				strWhere = strWhere + " and (" + billVo.getWherestring() + ")";
			else
				strWhere = billVo.getWherestring();
		}
		strWhere = nc.vo.mlr.pub.ToolKit.DeleteBusitype(strWhere);
		// String sql =
		// "select * from mlr_portbill_h ,mlr_portbill_b  where mlr_portbill_h.pk_portbill_h = mlr_portbill_b.pk_portbill_h and 1 = 1 and "
		// + strWhere ;
		// saber 2010-06-10
		String sql = "select distinct mlr_portbill_h.pk_portbill_h "
				+ "  from (select * from mlr_portbill_b  where nvl(tr, 0) = 0) mlr_portbill_b right join mlr_portbill_h "
				+ "  on mlr_portbill_h.pk_portbill_h =  mlr_portbill_b.pk_portbill_h where nvl(dr, "
				+ "  0) = 0 and 1=1 and " + strWhere + "";
		if (sql.contains("pk_corp"))
			sql = sql.replace("pk_corp", "mlr_portbill_h.pk_corp");
		if (sql.contains("dr"))
			sql = sql.replace("dr", "mlr_portbill_h.dr");
		if (sql.contains("tr"))
			sql = sql.replace("tr", "mlr_portbill_b.dr");
		// sql = sql.concat(" and isnull(mlr_portbill_b.dr,0)=0 ");
		// saber
		String sql2 = "select mlr_portbill_h.* from mlr_portbill_h where mlr_portbill_h.pk_portbill_h in ("
				+ sql + " and nvl(mlr_portbill_h.dr,0)=0)";
		ArrayList<PortbillHVO> al = (ArrayList<PortbillHVO>) MLRProxy
				.getIUAPQueryBS().executeQuery(sql2,
						new BeanListProcessor(PortbillHVO.class));
		// return HYPubBO_Client.queryByCondition(headClass, strWhere);
		return al.toArray(new PortbillHVO[al.size()]);
	}
}
